/*
 * NastaveniShowPanel.java
 *
 * Created on 19. �erven 2007, 20:50
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package cz.possoft.client.gui.panel.nastavenispodnipanel.panel;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import layout.TableLayout;
import cz.possoft.client.gui.dialog.VyzivaciListener;
import cz.possoft.client.gui.panel.nastavenispodnipanel.components.ConnectButton;
import cz.possoft.client.gui.panel.nastavenispodnipanel.components.ConnectLabel;
import cz.possoft.client.gui.panel.nastavenispodnipanel.components.ExceptionPanel;
import cz.possoft.client.gui.panel.nastavenispodnipanel.components.GamesComboBox;
import cz.possoft.client.gui.panel.nastavenispodnipanel.components.ListOfUsers;
import cz.possoft.client.threads.ConnectThread;
import cz.possoft.messages.PropertiesManager;
import cz.possoft.messages.Text;



/**
 *
 * @author mara
 */
public class NastaveniShowPanel extends JPanel {

    private static ConnectButton connectButton;
    private static ConnectLabel connectLabel;
    private static JTextField nameInputField;
    private static ExceptionPanel connectExceptionText;
    private static JButton vyzivaciButton;

    // vlakno ktere se pripojuje k serveru a bezi v nem veskera komunikace se serverem.
    private static ConnectThread connectThread;
    
    // komponenta se seznamem uzivatelu
    private static ListOfUsers listOfUsers;
    
    private static GamesComboBox gamesComboBox;

    //double[][] nastaveniSize = {{32, 63, 43, 83, 32}, {32, 22, 10, 22, 32, 32, 32, 32, 30, 10, 22, 54}};
    double[][] nastaveniSize = {{32, 83, 23, 83, 32},   {5, 22, 5, 22, 10, 22, 10, 22, 156, 10, 22, 0, 44}};
    /**
     * Creates a new instance of NastaveniShowPanel
     */
    public NastaveniShowPanel() {
        setLayout(new TableLayout(nastaveniSize));
        initContents();
    }

    public void initContents() {
        connectThread = new ConnectThread();
        add(new JLabel(PropertiesManager.getInstance().getMessageForKey(Text.NAME)), "1,1");
        nameInputField = new JTextField(15);
        add(nameInputField, "2,1,3,1");
        add(new JLabel(PropertiesManager.getInstance().getMessageForKey(Text.GAMES)), "1, 3");        
        add(getGamesComboBox(), "2,3,3,3");        
        connectButton = new ConnectButton(PropertiesManager.getInstance().getMessageForKey(Text.CONNECT));
        add(connectButton, "1,5,2,5");
        add(getConnectLabel(), "3,5,4,5");        
        
        //Object[] data = {"one", "<html><body><font color=blue>twoo</font></body></html>", "three", "four"};
        add(new JLabel(PropertiesManager.getInstance().getMessageForKey(Text.SEZNAM)),"1,7,2,7");           
        add(getListOfUsers().getScrollPane(), "1,8,3,8");
        vyzivaciButton = new JButton(PropertiesManager.getInstance().getMessageForKey(Text.VYZVAT));
        vyzivaciButton.addActionListener(new VyzivaciListener());
        vyzivaciButton.setEnabled(false);
        add(vyzivaciButton, "1,10,2,10");
        //add(new JButton("cc"), "1,9,3,9");        
        add(getConnectExceptionText(), "1,12,4,12");
    }

    public static ConnectThread getConnectThread() {
        return connectThread;
    }
    
    // nastaveni pripojovaciho vlakna. Pouzito v DialogThreadu a vola se pokud spadlo
    // spojeni se serverem.
    public static void setConnectThread(ConnectThread ct) {
        connectThread = ct;
        ct.start();
    }
    
    public static ConnectButton getConnectButton() {
        return connectButton;
    }
    
    public static JButton getVyzivaciButton() {
        return vyzivaciButton;
    }     
    
    public static GamesComboBox getGamesComboBox() {
    	if (gamesComboBox == null) {
    		gamesComboBox = new GamesComboBox();
    	}
    	return gamesComboBox;
    }
    
    public static ExceptionPanel getConnectExceptionText() {
    	if (connectExceptionText == null) {
    		connectExceptionText = new ExceptionPanel("");
    	}
    	return connectExceptionText;
    }
    
    public static ConnectLabel getConnectLabel() {
    	if (connectLabel == null) {
    		connectLabel = new ConnectLabel(PropertiesManager.getInstance().getMessageForKey(Text.NOTCONNECTED));
    	}
    	return connectLabel;    	
    }
    
    public static ListOfUsers getListOfUsers() {
    	if (listOfUsers == null) {
    		listOfUsers = new ListOfUsers(""); 
    	}
    	return listOfUsers;       	
    }

    // nastartuje vlakno pro spojeni. Volano z listeneru na pripojovaci button.
    // Vlakno se startuje jen pokud je null.
    public static void startConnectThread() {
        if (connectThread.getInitFlag() == false) {
            String trimClientName = nameInputField.getText().trim();
            if (trimClientName.length() == 0) {
                connectExceptionText.setText(PropertiesManager.getInstance().getMessageForKey(Text.ERROR_NO_NAME));
            } else {
                connectThread = new ConnectThread();
                connectThread.init(trimClientName);
            }
        } else {
            // pripad kdy je jmeno klienta ve vlakne prazdne (predtim zrejme
            // doslo k udalosti NAME_PRESENT a nyni klient znovu potvrzuje jmeno)
            if (connectThread.getClientName().equals("")) {                
                String trimClientName = nameInputField.getText().trim();
                if (trimClientName.length() == 0) {
                    connectExceptionText.setText(PropertiesManager.getInstance().getMessageForKey(Text.ERROR_NO_NAME));
                } else {
                    connectLabel.connected();
                    connectExceptionText.setText("");
                    connectThread.setClientName(trimClientName);
                    connectThread.sendToServer("CONNECT: " + trimClientName);
                }
            }
        }
    }
}
